package org.colin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.colin.dictPojo.DSubject;
import org.colin.mapper.DSubjectMapper;
import org.colin.mapper.DSubjectTypeMapper;
import org.colin.dictPojo.DSubjectType;
import org.colin.service.DSubjectTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * <p>
 * 试题标签表 服务实现类
 * </p>
 *
 * @author LiRuiFeng
 * @since 2021-01-09
 */
@Service
public class DSubjectTypeServiceImpl extends ServiceImpl<DSubjectTypeMapper, DSubjectType> implements DSubjectTypeService {


    @Autowired
    DSubjectTypeMapper dSubjectTypeMapper;
    @Autowired
    DSubjectMapper dSubjectMapper;

    @Override
    @Transactional
    public Integer deleteBatchSubjectType(List<Integer> ids) {

        Integer state = 1 ;
        try{
            for (int i = 0; i < ids.size(); i++) {
                Integer id = ids.get(i);
                QueryWrapper<DSubject> dSubjectQueryWrapper = new QueryWrapper<DSubject>();
                dSubjectQueryWrapper.eq("type",id);
                dSubjectQueryWrapper.eq("deleteMark","0");
                int count = dSubjectMapper.selectCount(dSubjectQueryWrapper);
                if(count > 0){
                    return 3 ;
                }
                dSubjectTypeMapper.deleteById(id);
            }
        }catch (Exception e){
            state = 2;
            e.printStackTrace();
            System.out.println(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            return state ;
        }

    }
}
